/*
 * TimePanel.java
 *
 * Created on 01 January 2004, 05:56
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.time;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.*;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.*;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.util.*;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

/**
 *
 * @author  Robert Crida Work
 */
public class TimePanel extends javax.swing.JPanel {
    
    private KeySession m_key;
    private TaskPK m_taskPK;
    private TimeTableModel m_modelTime;

    /** Creates new form TimePanel */
    public TimePanel(KeySession p_key, TaskPK p_taskPK, Set p_setPermissions) {
        initComponents();
        m_key = p_key;
        m_taskPK = p_taskPK;
        jTableEntries.getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jButtonCreate.setEnabled(p_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.TIME_CREATE))));
        jButtonEdit.setEnabled(p_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.TIME_EDIT))));
        jButtonDelete.setEnabled(p_setPermissions.contains(new PermissionPK(new Integer(PermissionBean.TIME_DELETE))));
    }
    
    void refresh() {
        try {
            jTextFieldNode.setText(StatelessRemoteClientSessionUtil.getSession().getNodePathName(m_key, new NodePK(m_taskPK.getTaskId())));
            Collection timeValues = StatelessRemoteClientSessionUtil.getSession().getTaskTimeValues(m_key, m_taskPK, new Date());
            m_modelTime.setTimeValues(timeValues);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabelNode = new javax.swing.JLabel();
        jTextFieldNode = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jButtonCreate = new javax.swing.JButton();
        jButtonEdit = new javax.swing.JButton();
        jButtonDelete = new javax.swing.JButton();
        jScrollPaneEntries = new javax.swing.JScrollPane();
        m_modelTime = new TimeTableModel();
        jTableEntries = new javax.swing.JTable(m_modelTime);

        setLayout(new java.awt.BorderLayout());

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
        jLabelNode.setText("Node:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jLabelNode, gridBagConstraints);

        jTextFieldNode.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel1.add(jTextFieldNode, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonCreate.setText("Create");
        jButtonCreate.setEnabled(false);
        jButtonCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCreateActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCreate);

        jButtonEdit.setText("Edit");
        jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonEdit);

        jButtonDelete.setText("Delete");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonDelete);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

        jScrollPaneEntries.setPreferredSize(new java.awt.Dimension(3, 160));
        jScrollPaneEntries.setViewportView(jTableEntries);

        add(jScrollPaneEntries, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void jButtonCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCreateActionPerformed
        // Add your handling code here:
        JTabbedPane tabbedPane = (JTabbedPane)getParent();
        TimeControl timeControl = new TimeControl(m_key, m_taskPK, null);
        tabbedPane.addTab("Create Time", timeControl);
        tabbedPane.setSelectedComponent(timeControl);        
    }//GEN-LAST:event_jButtonCreateActionPerformed

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        // Add your handling code here:
        try {
            int row = jTableEntries.getSelectedRow();
            if (JOptionPane.showConfirmDialog(null, "Are you sure you wish to delete the time entry?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                StatelessRemoteClientSessionUtil.getSession().taskTimeDelete(m_key, m_modelTime.getValue(row).getPrimaryKey());
                m_modelTime.removeRows(row, row);
            }
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
        // Add your handling code here:
        JTabbedPane tabbedPane = (JTabbedPane)getParent();
        TimeControl timeControl = new TimeControl(m_key, m_taskPK, m_modelTime.getValue(jTableEntries.getSelectedRow()).getPrimaryKey());
        tabbedPane.addTab("Edit Time", timeControl);
        tabbedPane.setSelectedComponent(timeControl);
    }//GEN-LAST:event_jButtonEditActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // Add your handling code here:
        refresh();
    }//GEN-LAST:event_formComponentShown
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCreate;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonEdit;
    private javax.swing.JLabel jLabelNode;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPaneEntries;
    private javax.swing.JTable jTableEntries;
    private javax.swing.JTextField jTextFieldNode;
    // End of variables declaration//GEN-END:variables
    
}
